package com.zpeb.pdd.utils;

import java.security.SecureRandom;
import java.util.UUID;

/**
 * @author VX5
 * @Title: MJC
 * @ProjectName graduation
 * @Description: TODO
 * @date ${DAT}15:14
 */
public class MyUUID implements java.io.Serializable, Comparable<UUID> {
    @Override
    public int compareTo(UUID o) {
        return 0;
    }

    private static final long serialVersionUID = -4856846361193249489L;

    /*
     * The most significant 64 bits of this UUID.
     *
     * @serial
     */
    private final long mostSigBits;

    /*
     * The least significant 64 bits of this UUID.
     *
     * @serial
     */
    private final long leastSigBits;

    /*
     * The random number generator used by this class to create random
     * based UUIDs. In a holder class to defer initialization until needed.
     */
    private static class Holder {
        static final SecureRandom numberGenerator = new SecureRandom();
    }

    // Constructors and Factories

    /*
     * Private constructor which uses a byte array to construct the new UUID.
     */
    private MyUUID(byte[] data) {
        long msb = 0;
        long lsb = 0;
        assert data.length == 8 : "data must be 8 bytes in length";
        for (int i=0; i<4; i++)
            msb = (msb << 4) | (data[i] & 0xff);
        for (int i=4; i<8; i++)
            lsb = (lsb << 4) | (data[i] & 0xff);
        this.mostSigBits = msb;
        this.leastSigBits = lsb;
    }

    /**
     * Constructs a new {@code UUID} using the specified data.  {@code
     * mostSigBits} is used for the most significant 64 bits of the {@code
     * UUID} and {@code leastSigBits} becomes the least significant 64 bits of
     * the {@code UUID}.
     *
     * @param  mostSigBits
     *         The most significant bits of the {@code UUID}
     *
     * @param  leastSigBits
     *         The least significant bits of the {@code UUID}
     */
    public MyUUID(long mostSigBits, long leastSigBits) {
        this.mostSigBits = mostSigBits;
        this.leastSigBits = leastSigBits;
    }


    public static MyUUID randomUUID() {
        SecureRandom ng = MyUUID.Holder.numberGenerator;

        byte[] randomBytes = new byte[8];
        ng.nextBytes(randomBytes);
        randomBytes[3]  &= 0x0f;  /* clear version        */
        randomBytes[3]  |= 0x40;  /* set to version 4     */
        randomBytes[4]  &= 0x3f;  /* clear variant        */
        randomBytes[4]  |= 0x80;  /* set to IETF variant  */
        return new MyUUID(randomBytes);
    }
    /**
     * Constructs a new {@code UUID} using the specified data.  {@code
     * mostSigBits} is used for the most significant 64 bits of the {@code
     * UUID} and {@code leastSigBits} becomes the least significant 64 bits of
     * the {@code UUID}.
     *
     * @param mostSigBits  The most significant bits of the {@code UUID}
     * @param leastSigBits The least significant bits of the {@code UUID}
     */

}
